const { Result } = require("../util/result");
const express = require('express');
const router = express.Router();
const db = require('../util/db');
const {qPromise} = require("../util/qPromise");
const {log} = require("debug");

/*todo-get type of strip*/
router.get('/strip', (req, res) => {
    let str=''
    if(req.query.cid){str='where cid = '+req.query.cid}
    let sql = 'select id,title,description,createTime,visitTimes from article '+str+' order by createTime desc'
    db.query(sql,[], (data) => {
        res.json(new Result(200, '查询成功', data))
    })
})

/*todo-get type of new strip*/
router.get('/newStrip', (req, res) => {
    let sql = 'select id,title,createTime,visitTimes from article order by createTime desc limit 10'
    db.query(sql,[], (data) => {
        res.json(new Result(200, '查询成功', data))
    })
})


/* todo-search */
router.get('/search', (req, res) => {
    if (!req.query.searchValue) req.query.searchValue = ''
    let sql = 'select id,title,description,createTime,visitTimes from article where title like ? or description like ? order by createTime desc'
    console.log(sql)
    db.query(sql, [`%${req.query.searchValue}%`, `%${req.query.searchValue}%`], (data) => {
        res.json(new Result(200, '查询成功', data))
    })
})

/*todo-get article detail by id */
router.get('/:id', async (req, res) => {
    let sql1 = 'update article set visitTimes=visitTimes+1 where id=?'

    let sql = 'select * from article where id=?'
    // try {
    //     const [data, d] = await Promise.all([
    //         qPromise(sql1, []),
    //         qPromise(sql, [])
    //     ]);
    //
    //     res.json(new Result(200, '查询成功', {data: data, total: total[0].total}));
    // } catch (error) {
    //     console.error(error);
    //     res.status(500).json(new Result(500, '查询失败', null));
    // }
    await db.query(sql1, [req.params.id], (data) => {})
    await db.query(sql, [req.params.id], (data) => {
        res.json(new Result(200, '查询成功', data))
    })
})

/*todo-搜索for管理 逆天东西*/

// router.get('/searchForAdmin', (req, res) => {
//     let str=''
//     if(req.query.cid!=0){str='and cid = '+req.query.cid}
//     let limit=10
//     if(!req.query.pageSize)limit=req.query.pageSize
//     let offset=0
//     if(!req.query.currentPage)offset=(currentPage-1)*limit
//     let sql=`select article.id,name,title,description,visitTimes,article.createTime from article,category where cid=category.id ${str} LIMIT ${limit} OFFSET ${offset};`
//     console.log(sql)
//     db.query(sql,[], (data) => {
//         res.json(new Result(200, '查询成功', data))
//     })
// })


/** todo- add article */
router.post('/addArticle', (req, res) => {
    let sql = 'insert into article(cid,title,description,main) values(?,?,?,?)'

    db.query(sql, [req.body.cid, req.body.title, req.body.description, req.body.main], (data) => {
        if (data.affectedRows === 1)
            res.json( new Result(200, '添加成功', null) )
        else res.json( new Result(500, '添加失败', null) )
    })
})



/*todo-通过id删除*/
router.delete('/delArticle', (req, res) => {
    let sql = 'delete from article where id=?'
    db.query(sql, [req.query.id], (data) =>{
        if (data.affectedRows === 1)
            res.json(new Result(200, '删除成功', null))
        else res.json(new Result(500, '删除失败', null))
    })
})


module.exports = router;